home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_02_10 / 2n10018a < prev    next >
Text File  |  1991-04-03  |  5KB  |  186 lines

  1.     TITLE    SPINLOCK - Spinlock Package for MP-Aware C Programs.
  2.  
  3. ;***    SPINLOCK - Spinlock Package for MP-Aware C Programs.
  4. ;
  5. ;   FUNCTIONAL DESCRIPTION.
  6. ;    This module is assembled to create an OBJ file that can be linked
  7. ;    with a large model C-language program to provide MP-aware spinlock
  8. ;    support.
  9. ;
  10. ;   MODIFICATION HISTORY.
  11. ;    S. E. Jones    91/04/05.    Original.
  12. ;
  13. ;   NOTICE: This code is hereby delivered to the public domain.
  14. ;
  15. ;   BUILD ENVIRONMENT.
  16. ;    MASM 5.10, no special switches.
  17.  
  18.     include spinlock.inc        ; spinlock macros.
  19.  
  20. DefProc MACRO    rtn            ; macro used to define procedures.
  21.     PUBLIC    rtn
  22. rtn    PROC    FAR
  23.     ENDM
  24.  
  25. EndProc MACRO    rtn            ; macro used to end procedures.
  26.     ret
  27. rtn    ENDP
  28.     ENDM
  29.  
  30. SPINLOCK SEGMENT PARA PUBLIC 'CODE'
  31.  
  32. ;***    _AllocateSpinLock - Initialize Storage Area as Spinlock Object.
  33. ;
  34. ;   FUNCTIONAL DESCRIPTION.
  35. ;    This routine is called by large-model C routines to initialize
  36. ;    a word of storage as a spinlock object.  The lock is set to the
  37. ;    released state.  A return code is architected, even though it
  38. ;    is not used in this particular package.  If the package is
  39. ;    changed to support spinlocks that are managed purely inside
  40. ;    the private part of this package, then it might be possible
  41. ;    to run out of handles to spinlocks.
  42. ;
  43. ;   ENTRY.
  44. ;    [bp+6,bp+8]  - FWA, word of storage to format as spinlock object.
  45. ;
  46. ;   EXIT.
  47. ;    AX    - 0 if success, else nonzero if failure.
  48. ;
  49. ;   USES.
  50. ;    none.
  51.  
  52.     ASSUME    CS:SPINLOCK, DS:NOTHING, ES:NOTHING, SS:NOTHING
  53. DefProc _AllocateSpinLock
  54.     push    bp
  55.     mov    bp, sp
  56.     push    si
  57.     push    ds
  58.     lds    si, [bp+6]        ; (DS:SI) = FWA, storage.
  59.     AllocateSpinLock <[si]>         ; initialize the lock.
  60.     sub    ax, ax            ; (AX) = 0, meaning success.
  61.     pop    ds
  62.     pop    si
  63.     pop    bp
  64. EndProc _AllocateSpinLock
  65.  
  66. ;***    _DeallocateSpinLock - Release Spinlock Object to System.
  67. ;
  68. ;   FUNCTIONAL DESCRIPTION.
  69. ;    This routine is called by large-model C routines to release
  70. ;    a word of storage used as a spinlock object.  The lock is
  71. ;    released, although this operation is superfluous in this
  72. ;    particular implementation.  Other implementations of this
  73. ;    package may actually require this routine to deallocate a
  74. ;    handle.
  75. ;
  76. ;   ENTRY.
  77. ;    [bp+6,bp+8]  - FWA, spinlock to deallocate.
  78. ;
  79. ;   EXIT.
  80. ;    AX    - 0 if success, else nonzero if failure.
  81. ;
  82. ;   USES.
  83. ;    none.
  84.  
  85.     ASSUME    CS:SPINLOCK, DS:NOTHING, ES:NOTHING, SS:NOTHING
  86. DefProc _DeallocateSpinLock
  87.     push    bp
  88.     mov    bp, sp
  89.     push    si
  90.     push    ds
  91.     lds    si, [bp+6]        ; (DS:SI) = FWA, storage.
  92.     DeallocateSpinLock <[si]>    ; deallocate the lock.
  93.     sub    ax, ax            ; (AX) = 0, meaning success.
  94.     pop    ds
  95.     pop    si
  96.     pop    bp
  97. EndProc _DeallocateSpinLock
  98.  
  99. ;***    _AcquireSpinLock - Acquire Spinlock for Exclusive Access.
  100. ;
  101. ;   FUNCTIONAL DESCRIPTION.
  102. ;    This routine is called by large-model C routines to acquire,
  103. ;    or gain control of, a registered spinlock.  This routine works
  104. ;    by calling the AcquireSpinLock macro, which repeatedly issues
  105. ;    an XCHG instruction on the lock with a nonzero value until a
  106. ;    zero value is returned.  Because Intel architecture guarantees
  107. ;    that XCHG will issue a LOCK across the bus, other processors
  108. ;    cannot interleave their XCHG cycles with ours.
  109. ;
  110. ;    This routine disables interrupts so that ISRs can also use locks.
  111. ;    If you don't need disabled interrupts, you should remove the CLI
  112. ;    instruction at the start of this routine, and also remove the STI
  113. ;    instruction at the end of the _ReleaseSpinLock routine.
  114. ;
  115. ;    A return status code is provided because in some implementations,
  116. ;    a spinlock's handle may be invalid.
  117. ;
  118. ;   ENTRY.
  119. ;    [bp+6,bp+8]  - FWA, spinlock object.
  120. ;
  121. ;   EXIT.
  122. ;    AX    - 0 if success, else nonzero if failure.
  123. ;
  124. ;   USES.
  125. ;    none.
  126.  
  127.     ASSUME    CS:SPINLOCK, DS:NOTHING, ES:NOTHING, SS:NOTHING
  128. DefProc _AcquireSpinLock
  129.     push    bp
  130.     mov    bp, sp
  131.     push    si
  132.     push    ds
  133.     lds    si, [bp+6]        ; (DS:SI) = FWA, storage.
  134.     cli                ; DISABLE INTERRUPTS.
  135.     AcquireSpinLock <[si]>        ; acquire the lock.
  136.     sub    ax, ax            ; (AX) = 0, meaning success.
  137.     pop    ds
  138.     pop    si
  139.     pop    bp
  140. EndProc _AcquireSpinLock
  141.  
  142. ;***    _ReleaseSpinLock - Release Spinlock to Other Processors.
  143. ;
  144. ;   FUNCTIONAL DESCRIPTION.
  145. ;    This routine is called by large-model C routines to release
  146. ;    control of a previously-acquired spinlock.  This routine works
  147. ;    by calling the ReleaseSpinLock macro, which simply stores a
  148. ;    zero in the lock in a single bus cycle.  Because the store
  149. ;    doesn't cause a read/modify/write cycle that could be interrupted,
  150. ;    we're safe.
  151. ;
  152. ;    This routine disables interrupts so that ISRs can also use locks.
  153. ;    If you don't need disabled interrupts, you should remove the STI
  154. ;    instruction at the end of this routine, and also remove the CLI
  155. ;    instruction at the beginning of the _AcquireSpinLock routine.
  156. ;
  157. ;   ENTRY.
  158. ;    [bp+6,bp+8]  - FWA, spinlock object.
  159. ;
  160. ;   EXIT.
  161. ;    AX    - 0 if success, else nonzero if failure.
  162. ;
  163. ;   USES.
  164. ;    none.
  165.  
  166.     ASSUME    CS:SPINLOCK, DS:NOTHING, ES:NOTHING, SS:NOTHING
  167. DefProc _ReleaseSpinLock
  168.     push    bp
  169.     mov    bp, sp
  170.     push    si
  171.     push    ds
  172.     lds    si, [bp+6]        ; (DS:SI) = FWA, storage.
  173.     ReleaseSpinLock <[si]>        ; release the lock.
  174.     sti                ; ENABLE INTERRUPTS.
  175.     sub    ax, ax            ; (AX) = 0, meaning success.
  176.     pop    ds
  177.     pop    si
  178.     pop    bp
  179. EndProc _ReleaseSpinLock
  180.  
  181. SPINLOCK ENDS
  182.  
  183.     END
  184.  
  185. Figure 2.  Spinlock Package for MP-Aware C Programs.
  186.